//
//  TopicdetailCell.m
//  XhlTrip
//
//  Created by dllo on 15/8/25.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TopicdetailCell.h"
#import "UIImageView+WebCache.h"

@implementation TopicdetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style  reuseIdentifier:reuseIdentifier];
    if (self) {
        self.CellDic = [NSMutableDictionary dictionary];
        
        self.mage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.mage];
        [_mage release];
        
        self.arerLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.arerLabel];
        [_arerLabel release];
        
        
        self.desTextView = [[UITextView alloc] init];
        [self.contentView addSubview:self.desTextView];
        self.desTextView.dataDetectorTypes = UIDataDetectorTypeLink;
        self.desTextView.editable = NO;
        _desTextView.font = [UIFont fontWithName:@"Verdana-Italic" size:16 * Width];
        _desTextView.scrollEnabled = NO;
        [_desTextView release];
        
        
        _arerLabel.font = [UIFont fontWithName:@"Verdana-BoldItalic" size:14 * Width];
        self.arerLabel.textColor = [UIColor colorWithRed:0/255.0f green:160/255.0f blue:255/205.0f alpha:1];
;
       
        
    }
    return self;
}

#pragma mark - 文字高度自适应
+ (CGFloat)heightWithText:(NSString *)text width:(CGFloat )width
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Verdana-Italic" size:17 * Width]} context:nil];
    return rect.size.height;
}

#pragma mark - 图片高度自适应
+ (CGFloat)heightWithImage:(NSDictionary *)magedic width:(CGFloat)width
{
    /* 等比例缩放算出高度 */
    CGFloat newh = [[magedic objectForKey:@"image_height"] floatValue] / [[magedic objectForKey:@"image_width"] floatValue] * width;
    /* 返回高度 */
    return newh;
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (![[self.CellDic objectForKey:@"image_url"] isEqualToString:@""]) {
        
        CGFloat photoHeight = [[self class] heightWithImage:self.CellDic width:self.contentView.frame.size.width - 20];
        [self.mage sd_setImageWithURL:[self.CellDic objectForKey:@"image_url"]];
        self.mage.frame = CGRectMake(10 * Width, 5 * Height, self.contentView.frame.size.width - 20 * Width, photoHeight);
    }else
    {
        self.mage.frame = CGRectMake(0, 0, 0, 0);
    }
    
    if ([[self.CellDic objectForKey:@"description"] isKindOfClass:[NSNull class]] || [[self.CellDic objectForKey:@"description"] isEqualToString:@"description"]) {
       
        self.desTextView.frame = CGRectMake(0, 0, 0, 0);
        
        
    }else{
        
       
        CGFloat heightDes = [[self class] heightWithText:[self.CellDic objectForKey:@"description"] width:self.contentView.frame.size.width - 20 * Width];

        self.desTextView.text = [self.CellDic objectForKey:@"description"];
        self.desTextView.frame = CGRectMake(10 * Width, self.mage.frame.size.height + 15 * Height, self.contentView.frame.size.width - 20 * Width, heightDes);
    }
    self.arerLabel.text = [[self.CellDic objectForKey:@"attraction"] objectForKey:@"name"];
    self.arerLabel.frame = CGRectMake(10 * Width, self.desTextView.frame.size.height + self.mage.frame.size.height + 20 * Height, self.contentView.frame.size.width - 20 * Width, 20 * Height);
}




- (void)dealloc
{
    [_CellDic release];
    [_desTextView release];
    [_mage release];
    [_arerLabel release];
    [super dealloc];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
